package cn.denvie.xfxblog;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SqlAgentTest {

    public static void main(String[] args) throws Exception {
        new SqlAgentTest().executeSql();
    }

    private void executeSql() throws Exception {
        String driver = "com.mysql.jdbc.NonRegisteringDriver";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/byte-agent?useUnicode=true&;characterEncoding=UTF8",
                "root", "root");
        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM USER WHERE id=? && username=?");
        pstmt.setInt(1, 1);
        pstmt.setString(2, "admin");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String username = rs.getString("username");
            String password = rs.getString("password");
            System.out.println("id: " + id + ", username: " + username + ", password: " + password);
        }
        rs.close();
        pstmt.close();
        conn.close();
    }

}
